package logic

import (
	"context"
	"encoding/json"
	"manuscript-service/models/cache"
	"manuscript-service/models/mysql"
	"time"

	"manuscript-service/manurpc/internal/svc"
	"manuscript-service/manurpc/manurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ManuListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewManuListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ManuListLogic {
	return &ManuListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 稿件列表
func (l *ManuListLogic) ManuList(in *manurpc.ManuListRequest) (*manurpc.ManuListResponse, error) {
	// todo: add your logic here and delete this line

	var list []*manurpc.ManuList

	result, _ := cache.Rdb.Get(l.ctx, "list").Result()

	if result == "" {
		list, _ = mysql.ManuList(mysql.DB, list)

		marshal, _ := json.Marshal(list)

		cache.Rdb.Set(l.ctx, "list", marshal, time.Minute*120)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &manurpc.ManuListResponse{
		List: list,
	}, nil
}
